Seaborn Exercises - Solutions

Time to practice your new seaborn skills! Try to recreate the plots below (don't worry about color schemes, just the plot itself.

The Data

We will be working with a famous titanic data set for these exercises. Later on in the Machine Learning section of the course, we will revisit this data, and use it to predict survival rates of passengers. For now, we'll just focus on the visualization of the data with seaborn:


In [19]:
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

In [27]:
sns.set_style('whitegrid')

In [28]:
titanic = sns.load_dataset('titanic')

In [40]:
titanic.head()


Out[40]:
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True

Exercises

Recreate the plots below using the titanic dataframe. There are very few hints since most of the plots can be done with just one or two lines of code and a hint would basically give away the solution. Keep careful attention to the x and y labels for hints.

Note! In order to not lose the plot image, make sure you don't code in the cell that is directly above the plot, there is an extra cell above that one which won't overwrite that plot!


In [42]:
# CODE HERE
# REPLICATE EXERCISE PLOT IMAGE BELOW
# BE CAREFUL NOT TO OVERWRITE CELL BELOW
# THAT WOULD REMOVE THE EXERCISE PLOT IMAGE!

In [41]:
sns.jointplot(x='fare',y='age',data=titanic)


Out[41]:
<seaborn.axisgrid.JointGrid at 0x11d0389e8>

In [43]:
# CODE HERE
# REPLICATE EXERCISE PLOT IMAGE BELOW
# BE CAREFUL NOT TO OVERWRITE CELL BELOW
# THAT WOULD REMOVE THE EXERCISE PLOT IMAGE!

In [44]:
sns.distplot(titanic['fare'],bins=30,kde=False,color='red')


Out[44]:
<matplotlib.axes._subplots.AxesSubplot at 0x11fc5ca90>

In [ ]:
# CODE HERE
# REPLICATE EXERCISE PLOT IMAGE BELOW
# BE CAREFUL NOT TO OVERWRITE CELL BELOW
# THAT WOULD REMOVE THE EXERCISE PLOT IMAGE!

In [45]:
sns.boxplot(x='class',y='age',data=titanic,palette='rainbow')


Out[45]:
<matplotlib.axes._subplots.AxesSubplot at 0x11f23da90>

In [ ]:
# CODE HERE
# REPLICATE EXERCISE PLOT IMAGE BELOW
# BE CAREFUL NOT TO OVERWRITE CELL BELOW
# THAT WOULD REMOVE THE EXERCISE PLOT IMAGE!

In [46]:
sns.swarmplot(x='class',y='age',data=titanic,palette='Set2')


Out[46]:
<matplotlib.axes._subplots.AxesSubplot at 0x11f215320>

In [ ]:
# CODE HERE
# REPLICATE EXERCISE PLOT IMAGE BELOW
# BE CAREFUL NOT TO OVERWRITE CELL BELOW
# THAT WOULD REMOVE THE EXERCISE PLOT IMAGE!

In [47]:
sns.countplot(x='sex',data=titanic)


Out[47]:
<matplotlib.axes._subplots.AxesSubplot at 0x11f207ef0>

In [ ]:
# CODE HERE
# REPLICATE EXERCISE PLOT IMAGE BELOW
# BE CAREFUL NOT TO OVERWRITE CELL BELOW
# THAT WOULD REMOVE THE EXERCISE PLOT IMAGE!

In [48]:
sns.heatmap(titanic.corr(),cmap='coolwarm')
plt.title('titanic.corr()')


Out[48]:
<matplotlib.text.Text at 0x11d72da58>

In [ ]:
# CODE HERE
# REPLICATE EXERCISE PLOT IMAGE BELOW
# BE CAREFUL NOT TO OVERWRITE CELL BELOW
# THAT WOULD REMOVE THE EXERCISE PLOT IMAGE!

In [49]:
g = sns.FacetGrid(data=titanic,col='sex')
g.map(plt.hist,'age')


Out[49]:
<seaborn.axisgrid.FacetGrid at 0x11d81c240>

Great Job!

That is it for now! We'll see a lot more of seaborn practice problems in the machine learning section!